<?php
/**
 * 安全过滤函数
 * @param $string
 * @return string
 */
function safe_replace( $string )
{
    $string = str_replace( '%20', '', $string );
    $string = str_replace( '%27', '', $string );
    $string = str_replace( '%2527', '', $string );
    $string = str_replace( '*', '', $string );
    $string = str_replace( '"', '&quot;', $string );
    $string = str_replace( "'", '', $string );
    $string = str_replace( '"', '', $string );
    $string = str_replace( ';', '', $string );
    $string = str_replace( '<', '&lt;', $string );
    $string = str_replace( '>', '&gt;', $string );
    $string = str_replace( "{", '', $string );
    $string = str_replace( '}', '', $string );
    $string = str_replace( '\\', '', $string );
    return $string;
}

/**
 * 字符串截取，支持中文和其他编码
 * @static
 * @access public
 * @param string $str 需要转换的字符串
 * @param string $start 开始位置
 * @param string $length 截取长度
 * @param string $charset 编码格式
 * @param string $suffix 截断显示字符
 * @return string
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
    if(function_exists("mb_substr"))
        return mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        return iconv_substr($str,$start,$length,$charset);
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
    if($suffix) return $slice."…";
    return $slice;
}

/**
* 读取缓存文件
* @param          fileName 缓存文件名
* @access         public    
*/
function get_cache($fileName)
{
    if(file_exists(C('MY_CACHE').'cache_'.$fileName.'.php'))
    {
        return F('cache_'.$fileName,'',C('MY_CACHE'));
    }else{return array();}
}

/**
 * seo设置
 * @access public
 * @param array 信息数组
 * @param string 用于标签页
 * @param int 首页标识
 */
function seo($arr='',$other='',$index=0)
{
    $options=get_cache('Options');
    $seo['title'] =isset($options['web_title']) && !empty($options['web_title']) ? $options['web_title'] : $options['web_name'];
    if($index==0)
    {
        $seo['title']=$options['web_name'];
    }
    $seo['keywords']=$options['web_keywords'];
    $seo['description']=strip_tags($options['web_description']);
    if($arr)
    {
        if(isset($arr['seo_title']) && !empty($arr['seo_title']))
        {
             $seo['title']=$arr['seo_title'].'-'.$options['web_name'];
        }else
            {
                if(isset($arr['title']) && !empty($arr['title']))
                {
                      $seo['title']=$arr['title'].'-'.$seo['title'];
                }
            }
        if(isset($arr['seo_keywords']) && !empty($arr['seo_keywords']))
        {
            $seo['keywords']=$arr['seo_keywords'];
        }else{$seo['keywords']='';} 
           if(isset($arr['seo_description']) && !empty($arr['seo_description']))
           {
                $seo['description']=strip_tags($arr['seo_description']);
           }else{$seo['description']=strip_tags($arr['brief']);}
    }
    foreach ($seo as $k=>$v)
    {
        $seo[$k] = str_replace(array("\n","\r"), '', $v);
    }
    if($other)
    {
        $seo['title']=$other.'-'.$seo['title'];
        $seo['keywords']=$other;
        $seo['description']=$other;
    }
    return $seo;
}

/**
 * 获取面包导航数据
 * @access public
 * @param int $id
 * @param array $data
 * @param array &$newArr 
 * @return array
 */
function get_pos($myId,$data,&$newArr)
{   
    $a=array();
    if(!isset($data[$myId]))return false;
    $newArr[]=$data[$myId];
    $pid=$data[$myId]['parent_id'];
    if(isset($data[$pid]))
    {
        get_pos($pid,$data,$newArr);
    }
    if(is_array($newArr))
    {
        krsort($newArr);
        foreach($newArr as $v)
        {
                $a[$v['id']] = $v;
        }
    }
    return $a;
}

function show_gravatar($email,$username)
{
  $default="http://www.0773linji.com/images/tou.png";
  $size=40;
  return '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email).
'&default='.$default.'&size='.$size.'&r=g" width="'.$size.'px" height="'.$size.'px" alt="'.$username.'" />';
}    


?>